package com.barber.controller;

import com.barber.entity.Style;
import com.barber.service.StyleService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@RestController  // @Controller+@ResponseBody @ResponseBody注解来返回一个JSON对象
@RequestMapping("/style")
public class StyleController {
    Logger logger = Logger.getLogger(StyleController.class);
    @Autowired
    StyleService styleService;

    @PostMapping()
    Map select(@RequestParam Map map, HttpServletRequest req) throws IOException {
//        Map m  = (Map) req.getSession().getAttribute("islogin");
//        logger.debug(m);//{password=22, adminid=1, usertype=0, fpemail=2966123846@qq.com, username=222}
        logger.debug(map);//{pageIndex=0, sortField=createtime, sortOrder=desc, pageSize=10}
//        map.put("key","");
        Map b=styleService.getStyles(map);

        logger.debug(b);
        return b;
    }
    @PostMapping("/upload")//org.springframework.web.multipart.MultipartException: Current request is not a multipart request
    String upload(@RequestParam("pic") MultipartFile pic,@RequestParam Map map)throws IOException {
//
        logger.debug(map);
        logger.debug(pic);


            String fileName = map.get("id") + "_.png";
            logger.debug(fileName);

            pic.transferTo(new File(fileName));//会直接传到application里的spring.servlet.multipart.location=D:/upload/imgs/

            if(fileName!=null){
            return  (fileName);}
            else {
                return ("update failure!");

            }
    }

    @PostMapping("/insert")//数据成功 图片空
    String insert(HttpServletRequest request, HttpServletResponse response,MultipartFile pic,@RequestBody Style style)throws IOException
    {

        logger.debug(style);
//        style.setId(UUID.randomUUID().toString());当发型设置为varchar时可以用uuid插入
        if (styleService.insertStyle(style)){
            logger.debug(style.getId());
//            String fileName =style.getId()+ "_.png";
//            logger.debug(fileName);
//            logger.debug(pic);//图片空能插入
//            pic.transferTo(new File(fileName));//会直接传到application里的spring.servlet.multipart.location=D:/upload/imgs/
//            logger.debug("cg"+style);
            return(""+style.getId());}
        else
            return("FAIL");
    }

    @PostMapping("/update")//
    String update(@RequestBody Style style,@RequestParam Map map)throws IOException {
//        修改不好修改图片只好修改图片信息
        logger.debug(map);
        logger.debug(style);
        if (styleService.updateStyle(style)) {
//            logger.debug(style.getId());
//            String fileName = style.getName() + "_.png";
//            logger.debug(fileName);
//            logger.debug(pic);
//            pic.transferTo(new File(fileName));//会直接传到application里的spring.servlet.multipart.location=D:/upload/imgs/
//            logger.debug("cg"+style);
            return ("success");
        } else {
            return ("update failure!");

        }
    }

    @GetMapping("/delete")//spring风格 前端传的是	url : "menu2/delete/"+menuid,//Spring风格
    String delete(HttpServletResponse response, HttpServletRequest request,@RequestParam Map map ) throws IOException {
        logger.debug(map);
        if (styleService.deleteStyle(map)) {
            return ("success!");
        } else {
            return("delete failure!");

        }
    }
    @GetMapping("/islogin")
    String islogin(HttpServletRequest req, HttpServletResponse resp, @RequestParam Map map) throws IOException {
        logger.debug(map);
        Map islogin=(Map) req.getSession().getAttribute("islogin");

        logger.debug(map);

        if(islogin==null) {
//			islogin.equals("mtc_null");
            //islogin = "mtc_null";
            return "mtc_null";
        }
        logger.debug(islogin);//{lxid=4, lxtel=null, lxpass=Yi123456, lxaddr=1, lxname=哒玲玲}
        return (islogin.toString());//{{=}}

    }
}
